package base;

import utils.CurrentLoginUser;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: lyy
 * Date: 2021/12/13 16:06
 */
public class CurrentUserUtils {

	private static final String USER = "user";

	private static ThreadLocal<Map<String, Object>> threadLocal = new ThreadLocal();

	public static void set(String key, Object value){
		Map<String, Object> map = threadLocal.get();
		if(map == null){
			map = new HashMap<>();
			threadLocal.set(map);
		}
		map.put(key, value);
	}

	public static Object get(String key){
		Map<String, Object> map = threadLocal.get();
		if(map == null){
			map = new HashMap<>();
			threadLocal.set(map);
		}
		return map.get(key);
	}

	public static void remove(){
		threadLocal.remove();
	}

	public static void setCurrentUser(Object user){
		set(USER, user);
	}

	public static CurrentLoginUser getCurrentUser(){
		return (CurrentLoginUser) get(USER);
	}

}
